package com.example.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.eduservice.entity.EduSubject;
import com.example.eduservice.entity.excel.SubjectData;
import com.example.eduservice.entity.subject.OneSubject;
import com.example.eduservice.entity.subject.TwoSubject;
import com.example.eduservice.listener.SubjectExcelListener;
import com.example.eduservice.mapper.EduSubjectMapper;
import com.example.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.servicebase.exceptionHandler.EduException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Anticipate
 * @since 2022-03-12
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    // @Autowired
    // private EduSubjectMapper eduSubjectMapper;

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            // 文件输入流
            InputStream inputStream = file.getInputStream();
            // 调用方法读取
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new EduException(20002, "添加课程分类失败");
        }
    }


    // 课程列表分类（树形）
    @Override
    public List<OneSubject> getSubjectList() {
        // 查询所有一级分类
        QueryWrapper<EduSubject> oneSubjectWrapper = new QueryWrapper<>();
        oneSubjectWrapper.eq("parent_id", "0");

        /*
         *继承ServiceImpl后  Spring自动将mapper进行了注入（
         * 只需使用  baseMapper....(wrapper)  或者（）this....(wrapper)
         * 不需要我们手动注入了
         *     @Autowired
         *     private EduSubjectMapper eduSubjectMapper;
         *     eduSubjectMapper....(wrapper)
         * */
        List<EduSubject> oneSubjectList = baseMapper.selectList(oneSubjectWrapper);
        // List<EduSubject> eduSubjects = eduSubjectMapper.selectList(oneSubjectWrapper);
        // System.out.println(oneSubjectList == eduSubjects);

        // 查询所有二级分类
        QueryWrapper<EduSubject> twoSubjectWrapper = new QueryWrapper<>();
        twoSubjectWrapper.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(twoSubjectWrapper);

        // 创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        // 封装一级分类
        // 将查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值
        // 封装到要求的list集合里面
/*        for (EduSubject eduSubject : oneSubjectList) {
            OneSubject oneSubject = new OneSubject();
            oneSubject.setId(eduSubject.getId());
            oneSubject.setTitle(eduSubject.getTitle());
            finalSubjectList.add(oneSubject);
        }*/

        for (EduSubject eduSubject : oneSubjectList) {
            OneSubject oneSubject = new OneSubject();
            // 使用Spring自带方法，将 eduSubject 里面各个属性的值 set 到 oneSubject 中
            // 和上面的 for 效果一样
            BeanUtils.copyProperties(eduSubject, oneSubject);


            // 在一级分类循环里面遍历查询所有的二级分类
            // 创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalList = new ArrayList<TwoSubject>();
            for (EduSubject twoSubject : twoSubjectList) {
                if (twoSubject.getParentId().equals(oneSubject.getId())) {
                    TwoSubject twoSubject1 = new TwoSubject();
                    BeanUtils.copyProperties(twoSubject, twoSubject1);
                    twoFinalList.add(twoSubject1);
                }
            }

            // 封装二级分类
            // 把一级分类下面苏哟欧二级分类放到一级分类里面
            oneSubject.setChildren(twoFinalList);

            finalSubjectList.add(oneSubject);

        }


        return finalSubjectList;

    }
}
